package com.sicau.smart_farmer.service;

import com.mongodb.client.result.UpdateResult;
import com.sicau.smart_farmer.common.TrainingProcessEnum;
import com.sicau.smart_farmer.entity.TrainingBatch;
import com.sicau.smart_farmer.entity.User;
import com.sicau.smart_farmer.entity.UserTrainingRecord;
import com.sicau.smart_farmer.repository.TrainingBatchRepository;
import com.sicau.smart_farmer.repository.UserRepository;
import com.sicau.smart_farmer.repository.UserTrainingRecordRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserTrainingRecordService {


    @Autowired
    private UserTrainingRecordRepository userTrainingRecordRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TrainingBatchRepository trainingBatchRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据用户id查询该用户所有的培训批次记录
     * @param userId 用户的id
     * @return
     */
    public List<UserTrainingRecord> selectUserTrainingRecordByUserId(String userId){
        List<UserTrainingRecord> userTrainingRecords = userTrainingRecordRepository.findByUser_Id(userId);
        return userTrainingRecords;
    }

    /**
     * 根据培训批次id查询所有的培训批次记录
     * @param trainingBatchId
     * @return
     */
    public List<UserTrainingRecord> selectUserTrainingRecordByTrainingBatchId(String trainingBatchId){
        List<UserTrainingRecord> userTrainingRecords = userTrainingRecordRepository.findByTrainingBatch_Id(trainingBatchId);
        System.out.println(trainingBatchId);
        System.out.println(userTrainingRecords);
        return  userTrainingRecords;
    }


    public List<UserTrainingRecord> getTrainingBatchRecordTrainingBatchIdByProcess(String trainingBatchId, String process){
        Query query = new Query();
        query.addCriteria(Criteria.where("training_batch.$id").is(new ObjectId(trainingBatchId)).and("training_process").is(process));
        List<UserTrainingRecord> userTrainingRecordList = mongoTemplate.find(query, UserTrainingRecord.class);
        return userTrainingRecordList;
    }

    /**
     * 新增用户批次记录
     * @param userId
     * @param trainingBatchId
     * @return
     */
    public UserTrainingRecord addUserTrainingRecord(String userId, String trainingBatchId) {
        //判断是否有对应批次以及用户
        Optional<TrainingBatch> trainingBatch = trainingBatchRepository.findById(trainingBatchId);
        Optional<User> user = userRepository.findById(userId);
        if (!trainingBatch.isPresent() || !user.isPresent()) {
            return null;
        }
        //初始化userTrainingRecord
        UserTrainingRecord userTrainingRecord = new UserTrainingRecord();
        userTrainingRecord.setTrainingBatch(trainingBatch.get());
        userTrainingRecord.setUser(user.get());
        userTrainingRecord.setTrainingProcess(TrainingProcessEnum.DURING.getStatus());

        return userTrainingRecordRepository.save(userTrainingRecord);
    }

    /**
     * 根据id删除用户参与的记录
     * @param userTrainingRecordId
     */
    public void deleteUserTrainingRecordById(String userTrainingRecordId){
        userTrainingRecordRepository.deleteById(userTrainingRecordId);
    }

    /**
     * 根据userTrainingRecordId更改process
     * @param userTrainingRecordId
     * @param process
     * @return
     */
    public boolean updateUserTrainingRecordProcess(String userTrainingRecordId , String process){
        UpdateResult result = mongoTemplate.upsert(new Query().addCriteria(Criteria.where("id").is(userTrainingRecordId)), new Update().set("trainingProcess", process), UserTrainingRecord.class);
        return result.wasAcknowledged();
    }

    /**
     * 根据用户id删除所有的记录
     * @param userId
     */
    public void deleteUserTrainingRecordByUserId(String userId){
        List<UserTrainingRecord> userTrainingRecords = selectUserTrainingRecordByUserId(userId);
        for (UserTrainingRecord item : userTrainingRecords){
            userTrainingRecordRepository.deleteById(item.getId());
        }
    }



}
